from rest_framework.views import Response,APIView
from goods.models import Vegetable
from utils.jwt_tool import JwtTool


class AddDishes(APIView):
    def post(self,request):
        v_classify2 = request.data.get('v_classify2')
        order_id = request.data.get('order_id')
        Vegetable.objects.create(v_classify2=v_classify2,
                                 order_id=order_id)
        return Response({'msg':'添加菜品类型成功', 'code':200})


class GetDishes(APIView):
    def get(self,request):
        # v_classify2 = request.data.get('v_classify2')
        # j = JwtTool()
        # token = request.headers.get('token')
        # payload = j.check_token(token)
        dishes_info = Vegetable.objects.all()
        list2 = []
        for i in dishes_info:
            dict2 = {
                'id':i.id,
                'v_classify2':Vegetable.v_classify2_type.get(str(i.v_classify2)),
                'v_classify':Vegetable.v_classify_type.get(str(i.v_classify)),
                'v_name':i.v_name
            }
            list2.append(dict2)
        return Response({'msg':'获取菜品分类成功', 'code':200, 'data':list2})


class UpdateDishes(APIView):
    def put(self,request):
        id = request.data.get('id')
        v_classify2 = request.data.get('v_classify2')
        order_id = request.data.get('order_id')
        Vegetable.objects.filter(id=id).update(v_classify2=v_classify2,order_id=order_id)
        return Response({'msg':'修改菜品类型成功', 'code':200})


class DelDishes(APIView):
    def delete(self,request):
        id = request.data.get('id')
        Vegetable.objects.filter(id=id).delete()
        return Response({'msg':'删除菜品类型成功', 'code':200})